// var util = require('../../utils/calendar.js')
var d = require('time.js')
var CN_Date = require('getCNDate.js');
var app = getApp()
var t = new Date();

Page({
    data: {
        monthNum: t.getMonth() + 1,
        yearNum: t.getFullYear(),
        MonthDayArray: [],
        toDate: t.getDate(),
        toMonth: t.getMonth() + 1,
        toYear: t.getFullYear(),
        fromToday: '今天',
        nongliDetail: CN_Date.GetLunarDay(t.getFullYear(), t.getMonth() + 1, t.getDate()),
    },

    onShow: function () {
        this.calcMonthDayArray();
    },
    // 生成日期
    calcMonthDayArray: function () {
        var data = this.data;
        var dateArray = d.paintCalendarArray(data.monthNum, data.yearNum);
        //如果不是当年当月，自动选中1号
        var notToday = (data.monthNum != t.getMonth() + 1 || data.yearNum != t.getFullYear());
        if (notToday) {
            for (var i = 0; i < dateArray[0].length; i++) {
                if (dateArray[0][i].num == 1) {
                    dateArray[0][i].isShowDayInfo = true;
                }
            }
        }
        this.setData({
            MonthDayArray: dateArray,
            toYear: notToday ? this.data.yearNum : t.getFullYear(),
            toMonth: notToday ? this.data.monthNum : t.getMonth() + 1,
            toDate: notToday ? 1 : t.getDate(),
            fromToday: notToday ? d.getFromTodayDays(1, data.monthNum - 1, data.yearNum) : '今天',
            nongliDetail: notToday ? CN_Date.GetLunarDay(data.yearNum, data.monthNum, 1) : CN_Date.GetLunarDay(t.getFullYear(), t.getMonth() + 1, t.getDate()),
        })
    },
    // 点击选择日期
    dateClick: function (e) {
        var dataset = e.currentTarget.dataset,
            that = this;
        wx.setStorageSync("currentDepartDate", {
            timestamp: dataset.timestamp,
            yearmothday: dataset.yearmothday,
            year: that.data.toYear,
            monthday: dataset.monthday,//阳历月日
            nonglimonthday: dataset.nlmonthday,//农历月日
            weekday: dataset.weekday//周几
        });
        wx.navigateBack({
            url: '../index/index'
        });
        /*var eId = e.currentTarget.id;
        var MonArray = this.data.MonthDayArray;
        var data = this.data;
        if (eId == "") return;
        //点击效果 ，且只能选中一个日期
        //FIX 这个遍历算法可以改进
        for (var i = 0; i < MonArray.length; i++) {
            for (var j = 0; j < MonArray[i].length; j++) {
                if (typeof (MonArray[i][j]) == 'string') {
                    continue;
                }
                if (MonArray[i][j].num == eId) {
                    MonArray[i][j].isShowDayInfo = !MonArray[i][j].isShowDayInfo;
                }
            }
        }

        for (var i = 0; i < MonArray.length; i++) {
            for (var j = 0; j < MonArray[i].length; j++) {
                if (typeof (MonArray[i][j]) == 'string' || MonArray[i][j].num == eId) {
                    continue;
                }
                MonArray[i][j].isShowDayInfo = false;
            }
        }

        this.setData({
            MonthDayArray: MonArray,
            toYear: data.yearNum,
            toMonth: data.monthNum,
            toDate: eId,
            fromToday: d.getFromTodayDays(eId, data.monthNum - 1, data.yearNum),
            nongliDetail: CN_Date.GetLunarDay(data.yearNum, data.monthNum, eId),
        })*/
    },
    //下一月
    nextMonth: function () {
        var that = this,
            m = that.data.monthNum,
            y = that.data.yearNum;
        if (m == 12) {
            that.setData({
                monthNum: 1,
                yearNum: y + 1,
            });
            console.log('nian',that.data.yearNum)
        } else {
            that.setData({
                monthNum: m + 1,
            });
        }
        that.calcMonthDayArray();
    },
    //上一月
    prevMonth: function () {
        var that = this,
            m = that.data.monthNum,
            y = that.data.yearNum;
        if (m == 1) {
            that.setData({
                monthNum: 12,
                yearNum: y - 1,
            });
        } else {
            that.setData({
                monthNum: m - 1,
            });
        }
        that.calcMonthDayArray();
    }

    /*monthTouch: function (e) {
        var beginX = e.target.offsetLeft;
        var endX = e.changedTouches[0].clientX;
        if (beginX - endX > 125) {
            this.nextMonth_Fn();
        }
        else if (beginX - endX < -125) {
            this.lastMonth_Fn();
        }
    }*/
})